package bl.controller;

import java.util.ArrayList;

import database.factory.BookDataFactory;
import database.factory.BookDataFactoryImpl;
import database.service.BookDataService;

import po.BookPO;
import vo.BookVO;
import bl.domain.BookSearchByCatalog;
import bl.service.BookSearchByCatalogBLService;

public class BookSearchByCatalogController implements BookSearchByCatalogBLService {
	
	private BookSearchByCatalog bookSearchByCatalog;
	
	public BookSearchByCatalogController() {
		bookSearchByCatalog = new BookSearchByCatalog();
	}
	
	private void init() {
		BookDataFactory bookDataFactory = new BookDataFactoryImpl();
		BookDataService bookDataService = bookDataFactory.getBookDataService();
		bookDataService.load();
		bookSearchByCatalog.setBookItr(bookDataService.getIterator());
	}

	@Override
	public ArrayList<BookVO> search(int catalogID) {
		init();
		ArrayList<BookVO> resultList = new ArrayList<BookVO>();
		while (bookSearchByCatalog.getBookItr().hasNext()) {
			BookPO book = bookSearchByCatalog.getBookItr().next();
			if (book.getCatalogID() == catalogID) {
				BookVO bookVO = new BookVO(book);
				resultList.add(bookVO);
			}
		}
		
		return resultList;
	}

}
